#ifndef __SOFT_UART_H
#define __SOFT_UART_H

#include "main.h"
#include "stdint.h"
#include "stdbool.h"

typedef enum {
    SOFT_UART_PARITY_NONE = 0,
    SOFT_UART_PARITY_EVEN,
    SOFT_UART_PARITY_ODD
} SoftUART_ParityTypeDef;

typedef struct {
    GPIO_TypeDef* tx_port;
    uint16_t tx_pin;
    TIM_HandleTypeDef* timer;
    uint32_t bit_time;
    SoftUART_ParityTypeDef parity;
    volatile uint16_t tx_frame;
    volatile uint8_t tx_bit_count;
    volatile bool is_sending;
} SoftUART_HandleTypeDef;

void SoftUART_Init(SoftUART_HandleTypeDef* huart, 
                   GPIO_TypeDef* tx_port, uint16_t tx_pin, 
                   TIM_HandleTypeDef* timer, 
                   SoftUART_ParityTypeDef parity);
void SoftUART_WriteByte(SoftUART_HandleTypeDef* huart, uint8_t data);
void SoftUART_WriteString(SoftUART_HandleTypeDef* huart, const char* str);
void SoftUART_WriteBuffer(SoftUART_HandleTypeDef* huart, uint8_t* buffer, uint16_t length);
bool SoftUART_IsBusy(SoftUART_HandleTypeDef* huart);
void SoftUART_TimerCallback(SoftUART_HandleTypeDef* huart);

#endif